<!DOCTYPE html>
<html>
<head>
  <!--
    If you are serving your web app in a path other than the root, change the
    href value below to reflect the base path you are serving from.

    The path provided below has to start and end with a slash "/" in order for
    it to work correctly.

    For more details:
    * https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base

    This is a placeholder for base href that will be replaced by the value of
    the `--base-href` argument provided to `flutter build`.
  -->
  <base href="$FLUTTER_BASE_HREF">

  <meta charset="UTF-8">
  <meta content="IE=Edge" http-equiv="X-UA-Compatible">
  <meta name="description" content="A new Flutter project.">

  <!-- iOS meta tags & icons -->
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="apple-mobile-web-app-title" content="flutter_demo">
  <link rel="apple-touch-icon" href="icons/Icon-192.png">

  <!-- Favicon -->
  <link rel="icon" type="image/png" href="favicon.png"/>

  <title>flutter_demo</title>
  <link rel="manifest" href="manifest.json">

  <script>
    // The value below is injected by flutter build, do not touch.
    var serviceWorkerVersion = null;
  </script>
  <!-- This script adds the flutter initialization JS code -->
  <script src="flutter.js" defer></script>
</head>
<body>
  <script>
    window.addEventListener('load', function(ev) {
      // Download main.dart.js
      _flutter.loader.loadEntrypoint({
        serviceWorker: {
          serviceWorkerVersion: serviceWorkerVersion,
        },
        onEntrypointLoaded: function(engineInitializer) {
          engineInitializer.initializeEngine().then(function(appRunner) {
            appRunner.runApp();
          });
        }
      });
    });

    // flutter 的 web 插件的具体逻辑
    // 在 flutter 中调用 flutterToJs() 后，就会调用 js 中的 webabcd_flutterToJs()
    window.webabcd_flutterToJs = async (method, arguments) => {
      const promise = new Promise((resolve, reject) => {
        if (method == "method1" ) {
          let returnValue = method1();
          // 返回给 flutter 的结果
          resolve(returnValue);

          // 用于演示 js 调用 flutter
          // 在 js 中调用 webabcd_jsToFlutter() 后，就会调用 flutter 中的 jsToFlutter()
          window.webabcd_jsToFlutter('js to flutter', "param");
        }

        else if (method == "method2" ) {
          let returnValue = method2(arguments);
          resolve(returnValue);
        }

        else if (method == "method3" ) {
          let name = arguments._get("name");
          let age = arguments._get("age");
          let returnValue = method3(name, age);
          resolve(returnValue);
        }

        else if (method == "method4" ) {
          let returnValue = method4(arguments);
          resolve(returnValue);
        }

        else if (method == "method5" ) {
          // 通过 Promise 的 reject() 返回一个异常信息，在 flutter 中可以通过 try/catch 捕获到一个 PlatformException 类型的异常
          reject("errorMessage");
        }
      });
      return promise;
    }

    function method1() {
      return "调用 method1 成功";
    }

    function method2(param) {
      return `调用 method2 成功 param:${param}`;
    }

    function method3(name, age) {
      return `调用 method3 成功 name:${name}, age:${age}`;
    }

    function method4(params) {
      return `调用 method4 成功 ${params.join(',')}`;
    }

  </script>
</body>
</html>